package webapp.mvc.finders;

import com.google.common.base.Function;
import com.zoowii.jpa_utils.extension.ExtendFinder;
import com.zoowii.jpa_utils.extension.Paginator;
import com.zoowii.jpa_utils.util.ListUtil;
import webapp.mvc.models.ProductCollectEntity;
import webapp.mvc.models.ProductEntity;
import webapp.mvc.models.ShopCollectEntity;
import webapp.mvc.models.ShopEntity;

import java.util.List;

public class ShopCollectFinder extends ExtendFinder<String, ShopCollectEntity> {
    public ShopCollectFinder(Class<?> kCls, Class<?> mCls) {
        super(kCls, mCls);
    }
    public ShopCollectEntity findByShopAndUser(String shopId, String userId) {
        return this.where().eq("shopId", shopId).eq("userId", userId).first();
    }
    public List<ShopEntity> getCollectedShopsByUser(String userId, Paginator paginator) {
        paginator = paginator.eq("userId", userId);
        List<ShopCollectEntity> shopCollectEntities = this.findByPaginator(paginator);
        List<ShopEntity> shopEntities = ListUtil.map(shopCollectEntities, new Function<ShopCollectEntity, ShopEntity>() {
            @Override
            public ShopEntity apply(ShopCollectEntity shopCollectEntity) {
                return ShopEntity.find.byId(shopCollectEntity.getShopId());
            }
        });
        shopEntities = ListUtil.filter(shopEntities, new Function<ShopEntity, Boolean>() {
            @Override
            public Boolean apply(ShopEntity shopEntity) {
                return shopEntity != null;
            }
        });
        return shopEntities;
    }
}
